<template>
	<view class="page friend_Page">
		<headerinfo></headerinfo>
		
		<view class="width60" style="background-color: #fff;">
			<u-navbar title="朋友圈" :autoBack="true"></u-navbar>
			<view class="bg_top"  :style="{ backgroundImage: `url(${userData.background?userData.background:'https://www.25175.net/images/mybg.jpg'})` }">
				<view class="cardbox">
					<view class="touxiang">
						<image class="tx" :src="userData.avatar" mode="aspectFill"></image>
					</view>
					<view class="userText">
						<view class="name" >
							{{userData.nickname}} 
						</view>
						<view class="numlist">
							<span class="list">
								<span>{{tongji.followeeCount}}</span>
								<span>关注</span>
							</span>
							<span class="list">
								<span>{{tongji.followerCount}}</span>
								<span>被关注</span>
							</span>
							<span class="list">
								<span>{{tongji.momentCount}}</span>
								<span>看过</span>
							</span>
						</view>
						
						<view class="numlist">
							<span class="list" >
								<span style="font-size: 30rpx;">{{userData.province}} {{userData.city}}</span>
							</span>
							
						</view>
					</view>
					<view class="tag">
						<span style="max-width: 180rpx;overflow: hidden; text-overflow: ellipsis; white-space: nowrap; " v-for="(item,index) in userData.tags" :key="index">{{item}} </span>
					</view>
				</view>
			</view>
			
			
			<view class="content" v-if="listData.length > 0">
				<view class="card" v-for="(item, index) in listData" :key="index">
					<new-item :ref="'videoRef_'+index" @friendVideoPlay="friendVideoPlay" @share="share" :new="item" :showDelete="false" :myarticle="false" :newIndex='index' ></new-item>
					</view>
					<view class="nomore">
						没有更多了 ~
					</view>
			</view>
			
			
			<view class="btns" v-if="query.username != username">
				<span class="with100" @click.stop="liaotian()"  v-if="ischeck">私聊</span>
				<span class="with100" @click="guanzhu" v-else>关注</span>
				
			</view>
		</view>
		
		
		<nodata top="30%" v-if="listData.length == 0 "></nodata>
		
		<uni-popup ref="popup" type="bottom">
			<share @closeDia="closeDia"></share>
		</uni-popup>
	</view>
</template>

<script>
	import share from '@/components/share/share.vue'
	import newItem from './components/new-item.vue';
	export default {
		components: {
			newItem,
			share
		},
		data() {
			return {
				userData:{},
				tongji:{},
				query:{
					lastDocId:"",
					pageSize:10,
					username:''
				},
				listData:[],
				ischeck:false,
				username:uni.getStorageSync('username')?uni.getStorageSync('username'):''
			};
		},
		onLoad(e) {
			
			console.log(e)
			this.query.username =e .username
			this.getData()
			this.getUserData()
			this.gettongji()
			this.checkguanzhu()
		},
		onReachBottom() {
				this.getData();
				this.getUserData()
				this.gettongji()
		},
		onShow() {
			
		},
		methods: {
			friendVideoPlay(e) {
				console.log('friendVideoPlay', e);
				Object.keys(this.$refs).forEach((refKey) => {
					if (refKey.startsWith('videoRef_')) {
						let parts = refKey.split("_");
						let index_ = parseInt(parts[1], 10);
						let indexMatch = (e == index_);
						if (!indexMatch) {
							this.$refs[refKey][0].videoPause();
						}
					}
				});
			},
			checkguanzhu(){
				
				let data = {
					focusedUsername:this.query.username,
				}
				this.$api2.checkFocused(data).then(res => {
					console.log(res)
					this.ischeck = res
				});
			},
			guanzhu(){
				let data = {
					followeeId:this.query.username,
					focus:true
				}
				
				if (this.userData.focused) {
					data.focus = false;
				}
				
				uni.showLoading({
					title: '加载中...'
				});
				this.$api2.followfocus(data).then(res => {
					if (data.focus) {
						this.$Show.show('关注成功');
						this.ischeck = true
					} else {
						this.$Show.show('取消成功');
						this.ischeck = false
					}
					uni.hideLoading();
					this.userData.focused = !this.userData.focused;
					this.$forceUpdate();
				});
			},
			liaotian(){
				let username = this.userData.username
				uni.navigateTo({
					url:`/pagesfriend/chat/chat?username=${username}`
				})
			},
			getUserData(){
			   	this.$api2.getUserInfo({username:this.query.username}).then(res => {
					this.userData = res
			   		console.log(res)
			   		console.log('其他人的用户信息')
			   	})
			},
			gettongji() {
				this.$api2.queryMyStatistics({username:this.query.username}).then(res => {
					console.log('统计')
					console.log(res)
					this.tongji = res
				})
			},
			getData(){
				this.$api2.getMyPublishedMoment(this.query).then(res => {
					if (res.lastDocId) {
						this.query.lastDocId = res.lastDocId;
						this.listData = [...this.listData, ...res.list];
					} else {
						this.query.lastDocId = '';
						this.listData = res.list;
					}
					
					
					console.log('this.listData')
					console.log(this.listData)
				})
			},
			// 分享到朋友圈
			onShareTimeline() {
				let data = {
					title: '来自好友的分享', // 默认是小程序的名称
					path: `/pagesfriend/tabbar/index`, // 默认是当前页面，必须是以‘/’开头的完整路径
					imageUrl: '', // 图片封面，本地文件路径、网络图片路，支持PNG及JPG，默认当前页面截图，显示图片长宽比是 5:4。
					success: res => {
						// 分享成功
						if (res.errMsg == 'shareAppMessage:ok') {}
					},
					fail: res => {
						// 用户取消
						if (res.errMsg == 'shareAppMessage:fail cancel') {}
						// 分享失败
						if (res.errMsg == 'shareAppMessage:fail') {}
					},
					complete: res => {}
				}
				return data
			},
			// 分享给好友
			onShareAppMessage(options) {
			
				// 设置菜单中的转发按钮触发转发事件时的转发内容
				var shareObj = {
					title: '来自好友的分享', // 默认是小程序的名称(可以写slogan等)
					desc: '', // 小程序的描述
					path: `/pagesfriend/tabbar/index`, // 默认是当前页面，必须是以‘/’开头的完整路径
					imageUrl: '', // 图片封面，本地文件路径、网络图片路，支持PNG及JPG，默认当前页面截图，显示图片长宽比是 5:4。
					success: function(res) {
						// 转发成功之后的回调
			
						if (res.errMsg == 'shareAppMessage:ok') {
							console.log(res)
			
						}
					},
			
				}
			
				// 返回shareObj
				return shareObj;
			},
			share() {
				this.$refs.popup.open('bottom')
			},
			closeDia() {
				this.$refs.popup.close()
			},
		}
	};
</script>

<style>
	page {
		background-color: #f2f2f2;
	}
</style>
<style lang="scss" scoped>
	
	.btns{
		position: fixed;
		bottom: 80rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 700rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 100;
		span{
			width: 330rpx;
			height: 90rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #25d4d0;
			color: #fff;
			border-radius: 50px;
			font-size: 32rpx;
		}
		.with100{
			width: 100%;
		}
	}
	
	.bg_top{
		height: 500rpx;
		width: 100%;
		background-size: 100%;
		position: relative;
		padding: 40rpx;
		padding-top: 170rpx;
		
		.ics{
			display: flex;
			align-items: center;
			justify-content: space-between;
			.icons{
				display: flex;
				align-items: center;
				.iconfont{
					color: #fff;
					margin-left: 30rpx;
				}
			}
			.iconfont{
				color: #fff;
				font-size: 44rpx;
			}
			
		}
		
		.cardbox{
			width: 90%;
			margin: 0 auto;
			display: block;
			position: absolute;
			left: 50%;
			bottom: 30rpx;
			transform: translateX(-50%);
			background-color: #ffffff8f;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 30rpx;
			.touxiang{
				.tx{
					width: 140rpx;
					height: 140rpx;
					border-radius: 50%;
					position: absolute;
					    left: 40rpx;
					    top: -40rpx;
				}
				
			}
			.userText{
				margin-left: 180rpx;
			}
			.name{
				font-size: 40rpx;
				display: flex;
				align-items: center;
				.iconfont{
					margin-left: 20rpx;
					font-size: 30rpx;
					color: #6b6b6b;
					font-weight: bold;
				}
			}
			
			.numlist{
				display: flex;
				.list{
					display: flex;
					align-items: center;
					margin-right: 20rpx;
					span:first-child{
					  font-size: 50rpx;
					  margin-right: 10rpx;
					}
				}
			}
		}
		
		.tag{
			margin-top: 30rpx;
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			span{
				padding: 10rpx 20rpx;
				background-color: #fff;
				border-radius: 40rpx;
				margin-right: 20rpx;
				    white-space: nowrap;
				display: inline-block;
				margin-bottom: 10rpx;
				.iconfont{
					font-size: 30rpx;
					display: inline-block;
				}
			}
		}
	}
	
</style>